package com.exchange.controller;

import com.exchange.dto.MerchantDto;
import com.exchange.entity.Merchant;
import com.exchange.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/merchant")
@CrossOrigin(origins = "*")
@PreAuthorize("hasRole('MERCHANT') or hasRole('ADMIN')")
public class MerchantController {
    
    @Autowired
    private MerchantService merchantService;
    
    @GetMapping("/profile")
    public ResponseEntity<MerchantDto> getMerchantProfile() {
        // In a real implementation, we'd get the current user's ID from the security context
        // For now, we'll return a placeholder - this would be based on the authenticated user
        Long currentUserId = 1L; // Placeholder - should come from security context
        return merchantService.findByUserId(currentUserId)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }
    
    @PutMapping("/profile")
    public ResponseEntity<MerchantDto> updateMerchantProfile(@RequestBody Merchant merchant) {
        // In a real implementation, we'd get the current user's ID from the security context
        // For now, we'll return a placeholder - this would be based on the authenticated user
        Long currentUserId = 1L; // Placeholder - should come from security context
        return merchantService.findByUserId(currentUserId)
                .map(existingMerchant -> {
                    // Update merchant details
                    Merchant updatedDetails = new Merchant();
                    updatedDetails.setBusinessName(merchant.getBusinessName());
                    updatedDetails.setBusinessLicense(merchant.getBusinessLicense());
                    updatedDetails.setContactPerson(merchant.getContactPerson());
                    updatedDetails.setAddress(merchant.getAddress());
                    updatedDetails.setSubscriptionType(merchant.getSubscriptionType());
                    updatedDetails.setSubscriptionStartDate(merchant.getSubscriptionStartDate());
                    updatedDetails.setSubscriptionEndDate(merchant.getSubscriptionEndDate());
                    updatedDetails.setMaxProducts(merchant.getMaxProducts());
                    updatedDetails.setMaxExchangeCodes(merchant.getMaxExchangeCodes());
                    updatedDetails.setMaxOrdersPerMonth(merchant.getMaxOrdersPerMonth());
                    
                    MerchantDto updatedMerchant = merchantService.updateMerchant(existingMerchant.getId(), updatedDetails);
                    return ResponseEntity.ok(updatedMerchant);
                })
                .orElse(ResponseEntity.notFound().build());
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<MerchantDto> getMerchantById(@PathVariable Long id) {
        return merchantService.findById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }
}